# coding: utf-8
import sys

from PySide6.QtCore import QPropertyAnimation, Property, QRect
from PySide6.QtGui import QColor, QPaintEvent, QPainter, Qt, QBrush, QConicalGradient, QPen
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit


class FlowingGradientFontWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_Gradient_Position = 0
        self.Global_Color = QColor(255, 255, 255, 255)
        self.color_str = ''
        self.m_Circle_Ratio = 0.0
        self.m_isForward = True

        self.animation = QPropertyAnimation(self, b"GradientPosition", self)
        self.animation.setDuration(5000)
        self.animation.setStartValue(0)
        self.animation.setEndValue(360)
        self.animation.finished.connect(self.animation.start)
        self.animation.start()

        self.animation2 = QPropertyAnimation(self, b"CircleRatio", self)
        self.animation2.setDuration(1000)
        self.animation2.setStartValue(self.m_Circle_Ratio)
        self.animation2.setEndValue(1.0)

        self.button = QPushButton("更改背景颜色", self)
        self.button.setGeometry(20, 20, 100, 30)
        self.button.clicked.connect(self.change_color)
        self.lineEdit = QLineEdit(self)
        self.lineEdit.textChanged.connect(self.setText)
        self.lineEdit.setGeometry(130, 20, 100, 30)

        self.resize(1440, 900)

    def setText(self, text: str):
        self.color_str = text
        self.update()

    def change_color(self):
        if self.m_isForward:
            self.forward()
            self.m_isForward = False
        else:
            self.backward()
            self.m_isForward = True

    def forward(self):
        self.animation2.setDirection(QPropertyAnimation.Direction.Forward)
        self.animation2.start()

    def backward(self):
        self.animation2.setDirection(QPropertyAnimation.Direction.Backward)
        self.animation2.start()

    def getGradientPosition(self):
        return self.m_Gradient_Position

    def setGradientPosition(self, value: int):
        if self.m_Gradient_Position == value:
            return
        self.m_Gradient_Position = value
        self.update()

    def setCircleRatio(self, value: float):
        if self.m_Circle_Ratio == value:
            return
        self.m_Circle_Ratio = value
        self.update()

    def getCircleRatio(self):
        return self.m_Circle_Ratio

    def paintEvent(self, event: QPaintEvent):
        painter = QPainter(self)
        w = self.width()
        h = self.height()
        sum_ = w + h

        painter.setRenderHints(
            QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform | QPainter.RenderHint.TextAntialiasing)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(QBrush(QColor(255, 255, 255, 255)))
        painter.drawRect(0, 0, w, h)

        painter.setBrush(QBrush(QColor(0, 0, 0, 255)))
        painter.drawEllipse(self.button.rect().bottomRight(), int(sum_ * self.m_Circle_Ratio),
                            int(sum_ * self.m_Circle_Ratio))

        gradient = QConicalGradient(w / 2, h, self.m_Gradient_Position)
        gradient.setColorAt(0, QColor(255, 167, 69, 255))
        gradient.setColorAt(0.125, QColor(254, 134, 159, 255))
        gradient.setColorAt(0.25, QColor(239, 122, 200, 255))
        gradient.setColorAt(0.375, QColor(160, 131, 237, 255))
        gradient.setColorAt(0.5, QColor(67, 174, 255, 255))
        gradient.setColorAt(0.625, QColor(160, 131, 237, 255))
        gradient.setColorAt(0.75, QColor(239, 122, 200, 255))
        gradient.setColorAt(0.875, QColor(254, 134, 159, 255))
        gradient.setColorAt(1, QColor(255, 167, 69, 255))
        painter.setBrush(gradient)

        font = painter.font()
        font.setPixelSize(h // 5)
        font.setFamily("微软雅黑")
        font.setBold(True)
        painter.setFont(font)
        painter.setPen(QPen(QBrush(gradient), 2))
        painter.drawText(QRect(0, 0, w, h), Qt.AlignmentFlag.AlignCenter, self.color_str)
        painter.end()

    GradientPosition = Property(int, getGradientPosition, setGradientPosition)
    CircleRatio = Property(float, getCircleRatio, setCircleRatio)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = FlowingGradientFontWidget()
    widget.show()
    sys.exit(app.exec())
